package ezy.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import ezy.widget.R;


public class SettingView extends RelativeLayout {

    private String mIcon = "";
    private String mTitle = "";
    private String mSummary = "";
	private String mValue = "";
    private int mVauleColor;
    private int mIconVisibility = 0;
    private int mArrowVisibility = 0;
    private float mIconSize;
    private float mValueSize;
    private float mIconTextSize = 14;
    TextView txtIcon;
    TextView txtTitle;
    TextView txtSummary;
    TextView txtValue;
    TextView txtArrow;
    private Drawable mDrawable;

	public SettingView(Context context) {
		super(context);
		this.readStyleParameters(context, null);
	}

	public SettingView(Context context, AttributeSet attributeSet) {
		super(context, attributeSet);
		this.readStyleParameters(context, attributeSet);
	}

	public SettingView(Context context, AttributeSet attributeSet, int defStyle) {
		super(context, attributeSet, defStyle);
		this.readStyleParameters(context, attributeSet);
	}


    private <T extends View> T find(int id) {
        return (T)findViewById(id);
    }
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        txtIcon = find(android.R.id.icon);
        txtTitle = find(android.R.id.title);
        txtValue = find(android.R.id.text1);
        txtArrow = find(android.R.id.icon2);
        txtSummary = find(android.R.id.summary);

        if (isInEditMode()) {
            txtIcon.setText("G");
            txtArrow.setText(">");
        } else {
            if(mDrawable!=null) {
                txtIcon.setText("");
                mDrawable.setBounds(0,0,(int)mIconSize,(int)mIconSize);
                txtIcon.setCompoundDrawables(mDrawable,null,null,null);
            }else{
                txtIcon.setText(mIcon == null ? "" : mIcon);
            }
        }

        txtTitle.setText(mTitle);
        txtValue.setText(mValue);
        txtValue.setTextColor(mVauleColor);
        txtValue.setTextSize(mValueSize);
        txtSummary.setText(mSummary);
        txtSummary.setVisibility(TextUtils.isEmpty(mSummary) ? GONE : VISIBLE);
        txtIcon.setVisibility(mIconVisibility == 0 ? VISIBLE:(mArrowVisibility==1?INVISIBLE:GONE));
        txtArrow.setVisibility(mArrowVisibility == 0?VISIBLE:(mArrowVisibility==1?INVISIBLE:GONE));
    }

    private void readStyleParameters(Context context, AttributeSet attributeSet) {
        TypedArray a = context.obtainStyledAttributes(attributeSet, R.styleable.SettingView);
        int layoutId = 0;
        try {
            layoutId = a.getResourceId(R.styleable.SettingView_settingLayout, NO_ID);
            mIcon = a.getString(R.styleable.SettingView_settingIcon);
            mTitle = a.getString(R.styleable.SettingView_settingTitle);
            mValue = a.getString(R.styleable.SettingView_settingValue);
            mSummary = a.getString(R.styleable.SettingView_settingSummary);
            mDrawable=a.getDrawable(R.styleable.SettingView_settingDrawable);
            mIconVisibility = a.getInteger(R.styleable.SettingView_settingIconVisibility, 0);
            mArrowVisibility = a.getInteger(R.styleable.SettingView_settingArrowVisibility, 0);
            mIconSize = a.getDimension(R.styleable.SettingView_settingIconSize, mIconTextSize);
            mVauleColor=a.getColor(R.styleable.SettingView_settingValueColor, Color.BLACK);
            mValueSize=a.getDimension(R.styleable.SettingView_settingValueSize,14);
        } finally {
            a.recycle();
        }
        inflate(context, layoutId, this);
    }

    public void setIcon(String value) {
        mIcon = value;
        txtIcon.setText(value);
    }
    public void setTitle(String value) {
        mTitle = value;
        txtTitle.setText(value);
    }
    public void setValue(String value) {
        mValue = value;
        txtValue.setText(value);
    }
    public void setSummary(String value) {
        mSummary = value;
        txtSummary.setText(value);
        txtSummary.setVisibility(TextUtils.isEmpty(mSummary) ? GONE : VISIBLE);
    }


}